package com.example.api.service.impl;

import com.example.api.dao.DistributionDao;
import com.example.api.dao.TollDao;
import com.example.api.model.constants.LogisticsConstants;
import com.example.api.model.entity.Distribution;
import com.example.api.model.entity.Toll;
import com.example.api.model.support.RespBean;
import com.example.api.service.TollService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class TollServiceImpl implements TollService {

    @Resource
    private TollDao tollDao;
    
    @Resource
    private DistributionDao distributionDao;

    @Override
    public List<Toll> findAll(String name) {
        return tollDao.findAll(name);
    }

    @Override
    public RespBean update(Toll toll) {
        Distribution distribution = distributionDao.selectByPrimaryKey(toll.getDid());
        if (LogisticsConstants.DISTRIBUTION_STATUS_MINUS_1.equals(distribution.getStatus())) {
            return RespBean.error("配送未通过，无需支付");
        }
        toll.setPayTime(new Date());
        if (tollDao.updateByPrimaryKeySelective(toll) > 0) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @Override
    public Toll findById(Integer id) {
        return tollDao.findById(id);
    }

}